package com.lagou.controller;

import com.lagou.mapper.ArticleMapper;
import com.lagou.pojo.Article;
import com.lagou.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;

@SuppressWarnings("ALL")
@Controller
public class BlogController {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleRepository articleRepository;

    private final Sort sort = Sort.by(Sort.Direction.DESC, "id");

    @RequestMapping("/blog")
    public String toLoginPage(Model model, Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, 5, sort);
        Page<Article> list = articleRepository.findAll(pageable);
        model.addAttribute("articles", list);
        return "client/index";
    }


}
